﻿using AutoMapper;
using Durian.API.Application.Commands.Process;
using IMES.Common.Domain.DurianModel;
using IMES.Common.Dtos;
using IMES.Common.Infrastructure;
using MediatR;
using System.Reflection.PortableExecutable;

namespace Durian.API.Application.Commands.Mission
{
    public class MissionSelectHandler : IRequestHandler<MissionSelectCommand, Result>
    {
        private readonly IMapper mapper;
        private readonly ILogger<MissionSelectHandler> logger;
        private readonly IBaseRepositorys<ProcessManagement> processManagementRepository;
        private readonly IBaseRepositorys<Tasks> tasksRepository;

        public MissionSelectHandler(IBaseRepositorys<ProcessManagement> processManagementRepository, IMapper mapper, ILogger<MissionSelectHandler> logger, IBaseRepositorys<Tasks> tasksRepository)
        {
            this.processManagementRepository = processManagementRepository;//工序管理
            this.mapper = mapper;
            this.logger = logger;//日志
            this.tasksRepository = tasksRepository;//任务管理
        }

        /// <summary>
        ///任务管理显示和查询
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(MissionSelectCommand request, CancellationToken cancellationToken)
        {
            var res = new Result() { Code = (int)CodeEnum.search };
            var tasks = tasksRepository.GetAll().Where(x => x.Deletes == true);
            var process = processManagementRepository.GetAll();
            var listQuery = (from a in tasks
                             join b in process
                             on a.ProcessID equals b.ProcessID
                             select new TaskDto
                             {
                                 TasksId = a.TasksId,//任务编号
                                 ProcessName = b.ProcessName,//工序名称
                                 ProcessNumber = b.ProcessNumber,//工序编号
                                 ReportingAuthority = b.ReportingAuthority,//报工权限
                                 Matching = b.Matching,//报工数配比
                                 AdverseList = b.AdverseList,//不良品相列表
                                 PlanneStartTime = a.PlanneStartTime,//计划开始时间
                                 PlanneEndTime = a.PlanneEndTime,//计划结束时间
                                 PlannedNumber = a.PlannedNumber,//计划数
                                 GoodScore = a.GoodScore,//良品数
                                 DefectiveQuantity = a.DefectiveQuantity,//不良品数
                                 ActualStartTime = a.ActualStartTime,//实际开始时间
                                 ActualEndTime = a.ActualEndTime//实际结束时间
                             });
            //查询条件
            //工序名称
            if (!string.IsNullOrEmpty(request.ProcessName))
            {
                listQuery = listQuery.Where(x => x.ProcessName.Contains(request.ProcessName));
            }
            //工序编码精确查询
            if (request.ProcessNumber != 0)
            {
                process = process.Where(a => a.ProcessNumber == request.ProcessNumber);
            }
            //不良品相列表
            if (!string.IsNullOrEmpty(request.AdverseList))
            {
                listQuery = listQuery.Where(x => x.AdverseList.Contains(request.AdverseList));
            }
            //计划开始时间
            if (!string.IsNullOrEmpty(request.start1))
            {
                listQuery = listQuery.Where(x => x.PlanneStartTime >= DateTime.Parse(request.start1));
            }
            if (!string.IsNullOrEmpty(request.end1))
            {
                listQuery = listQuery.Where(x => x.PlanneStartTime <= DateTime.Parse(request.end1));
            }
            //计划结束时间
            if (!string.IsNullOrEmpty(request.start2))
            {
                listQuery = listQuery.Where(x => x.PlanneEndTime >= DateTime.Parse(request.start2));
            }
            if (!string.IsNullOrEmpty(request.end2))
            {
                listQuery = listQuery.Where(x => x.PlanneEndTime <= DateTime.Parse(request.end2));
            }
            //计划数
            if (request.PlannedNumber != 0)
            {
                listQuery = listQuery.Where(x => x.PlannedNumber == request.PlannedNumber);
            }
            //良品数
            if (request.GoodScore != 0)
            {
                listQuery = listQuery.Where(x => x.GoodScore == request.GoodScore);
            }
            //不良品数
            if (request.DefectiveQuantity != 0)
            {
                listQuery = listQuery.Where(x => x.DefectiveQuantity == request.DefectiveQuantity);
            }
            //实际开始时间
            if (!string.IsNullOrEmpty(request.start3))
            {
                listQuery = listQuery.Where(x => x.ActualStartTime >= DateTime.Parse(request.start3));
            }
            if (!string.IsNullOrEmpty(request.end3))
            {
                listQuery = listQuery.Where(x => x.ActualStartTime <= DateTime.Parse(request.end3));
            }
            //实际结束时间
            if (!string.IsNullOrEmpty(request.start4))
            {
                listQuery = listQuery.Where(x => x.ActualEndTime >= DateTime.Parse(request.start4));
            }
            if (!string.IsNullOrEmpty(request.start4))
            {
                listQuery = listQuery.Where(x => x.ActualEndTime >= DateTime.Parse(request.start4));
            }
            //创建时间
            if (!string.IsNullOrEmpty(request.start5))
            {
                listQuery = listQuery.Where(x => x.CreateDate >= DateTime.Parse(request.start5));
            }
            if (!string.IsNullOrEmpty(request.end5))
            {
                listQuery = listQuery.Where(x => x.CreateDate >= DateTime.Parse(request.end5));
            }
            res.Code = (int)CodeEnum.success;
            res.Data = listQuery;
            return res;
        }
    }
}
